<div class="alert">
<?php
require_once('includes/user_manage.php');

$user->isAdmin or exit("Área reservada a administradores");

if (isset($_POST["category_id"])) {
	$my_category=new admin_category();
	$my_category->id=$_POST["category_id"];
	$my_category->db_load();
	$my_category->db_load_items();
	$my_category->db_load_subcategories();
	$removed=false;
	if (count($my_category->categories)) {
	?>
		Error: La categoria contiene subcategorías. Debe borrar primero las subcategorías.
	<?php
	}
	else if (count($my_category->items)) {
	?>
		Error: La categoria contiene productos. Debe borrar primero los productos.
	<?php
	}
	else	{
		$removed=$my_category->db_delete();
		if ($removed) {
		?>
			Se ha borrado la categoria.
		<?php
		}
		else {
		?>
			Error desconocido.
		<?php
		}
	}
?>

<script type="text/javascript">
<?php
	if ($removed) {
?>
var categoryId=<?php print $_POST["category_id"]; ?>;
	var catalogPointer=myCatalog;
	catalogPointer=catalogPointer.moveToChildren("categoryId", categoryId);
	if (!catalogPointer.myCategory.id) {
		catalogPointer=catalogPointer.moveToChildren("selected");
		catalogPointer=catalogPointer.moveToChildren("categoryId", categoryId);
	}
	var itWasSelected=false;
	if (catalogPointer.myCategory.selected) itWasSelected=true;
	catalogPointer=catalogPointer.parentNode;
	var newChildren=new Array();
	for (var i in catalogPointer.children) {
		if (catalogPointer.children[i].myCategory.id != categoryId) newChildren.push(catalogPointer.children[i]);
	}
	catalogPointer.children=newChildren;
	if (catalogPointer.myCategory.id>0) catalogPointer.refreshCategoriesListView("flaps");
	else catalogPointer.refreshCategoriesListView("categories");
	
	if (itWasSelected) {
		if (catalogPointer.children[0]) {
			catalogPointer=catalogPointer.children[0];
			catalogPointer.myCategory.myActivate();
			catalogPointer.myCategory.myActionButton.request();
		}
		else {
			catalogPointer.myCategory.myActionButton.request();
		}
	}
	
<?php
	}
?>

window.setTimeout(removeAlert,2000,document.getElementById('delCategory'));
function removeAlert(alert) {
	alert.parentNode.removeChild(alert);
}
</script>
<?php
}
else {
?>
	<p id="alertMsg"></p>
	<form method="post" action="?section=del_category" myFor="delCategory" id="delCategoryForm">
	<input type="hidden" value="<?php print $_GET["category_id"]; ?>" name="category_id">
	<table class="mytable">
		<tr>
			<td><input type="button" value="No borrar" myFor="delCategory"></td>
			<td><input type="submit" value="Borrar" name="submitButton"></td>
		</tr>
	</table>
	</form>
<script type="text/javascript">
var categoryId=<?php print $_GET["category_id"]; ?>;

	var found=false;
	var catalogPointer=myCatalog;
	catalogPointer=catalogPointer.moveToChildren("categoryId", categoryId);
	if (!catalogPointer.myCategory.id) {
		catalogPointer=catalogPointer.moveToChildren("selected");
	}
	catalogPointer=catalogPointer.moveToChildren("categoryId", categoryId);

var alertMsg="No se puede borrar la categoría: "+ catalogPointer.name + ".";
var myMsg=document.getElementById("alertMsg");
if (catalogPointer.children.length >0) {
	alertMsg +=" La categoría contiene subcategorías. Borre primero las subcategorías";
	myMsg.innerHTML=alertMsg;
	var myForm=document.getElementById("delCategoryForm");
	myForm.submitButton.disabled=true;
}
else if (catalogPointer.items.length>0) {
	alertMsg +=" La categoría contiene productos. Borre primero los productos.";
	myMsg.innerHTML=alertMsg;
	var myForm=document.getElementById("delCategoryForm");
	myForm.submitButton.disabled=true;
}
else {
	alertMsg="¿Está seguro de querer borrar la categoría "+ catalogPointer.myCategory.name + "?";
	myMsg.innerHTML=alertMsg;
}
</script>
<?php
}
?>
</div>